﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace wpfHungryCitizen
{
    /// <summary>
    /// Interaction logic for Window3.xaml
    /// </summary>
    public partial class Window3 : Window
    {
        public Window3()
        {
            InitializeComponent();
        }
        bool m_moving = false;
        Shape m_cc;
        bool m_atLeft = false;
        private void canvas1_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            return;
            TranslateTransform tt = new TranslateTransform(0,0);
            tt.X = e.GetPosition(w3).X - Canvas.GetLeft(ellipse1); 
            tt.Y = e.GetPosition(w3).Y - Canvas.GetTop(ellipse1);
            
            ellipse1.RenderTransform = tt;

        }

        private void canvas1_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            //object o = (e.OriginalSource as Ellipse ).Name ;
            m_moving = false;
            m_cc = e.OriginalSource  as Shape;
            m_moving = true;
        }

        private void canvas1_PreviewMouseMove(object sender, MouseEventArgs e)
        {
            if (m_moving && m_cc != null)
            {
                Point loc = e.GetPosition( canvas1);
                double x = (double)ellipse1.GetValue(Canvas.LeftProperty);
                loc = ellipse1.TranslatePoint(new Point(loc.X - x, 0), m_cc);
                ellipse1.SetValue(Canvas.LeftProperty,loc.X );
                //ellipse1.SetValue(Canvas.TopProperty, loc.Y);
            }

        }
        private void canvas1_PreviewMouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            if (m_atLeft)
            {
                moveRight(m_cc);
            }
            else
            {
                moveLeft(m_cc);
            }
            m_moving = false;
            m_cc = null;
            m_atLeft = !m_atLeft;
        }
        void moveRight(Shape obj)
        {
            TranslateTransform moveShape = new TranslateTransform(0, 0);
            (obj as Shape).RenderTransform = moveShape;
            DoubleAnimation myAnimation =
            new DoubleAnimation(
                200, // "From" value
                1, // "To" value 
                new Duration(TimeSpan.FromSeconds(2))
            );
            myAnimation.AutoReverse = false;
            myAnimation.FillBehavior = FillBehavior.HoldEnd;
            myAnimation.EasingFunction = new BounceEase();
            myAnimation.DecelerationRatio = .2;
            myAnimation.AccelerationRatio = .2;
            AnimationClock clock = myAnimation.CreateClock();
            moveShape.ApplyAnimationClock(TranslateTransform.XProperty, clock);

        }
        void moveLeft(Shape obj)
        {
            TranslateTransform moveShape = new TranslateTransform(0, 0);
            (obj as Shape).RenderTransform = moveShape;
            DoubleAnimation myAnimation =
            new DoubleAnimation(
                1, // "From" value
                200, // "To" value 
                new Duration(TimeSpan.FromSeconds(2))
            );
            myAnimation.AutoReverse = false;
            myAnimation.FillBehavior = FillBehavior.HoldEnd;
            myAnimation.EasingFunction = new ElasticEase();// new BounceEase();
            myAnimation.DecelerationRatio = .2;
            myAnimation.AccelerationRatio = .2;
            AnimationClock clock = myAnimation.CreateClock();
            moveShape.ApplyAnimationClock(TranslateTransform.XProperty, clock);

        }
    }
}
